<template>
    <section>
        <!--工具条-->
        <el-col :span="24" class="toolbar" style="padding-bottom: 0px;">
            <el-form :inline="true" :model="filters">
                <el-form-item>
                    <el-input v-model="filters.keyword" placeholder="关键字搜索"></el-input>
                </el-form-item>
                <el-form-item>
                    <el-button type="primary" v-on:click="search">查询</el-button>
                </el-form-item>
                <el-form-item>
                    <el-button type="primary" @click="handleAdd">新增</el-button>
                </el-form-item>
            </el-form>
        </el-col>

        <!--列表-->
        <el-table :data="systemdictionarydetails" highlight-current-row v-loading="listLoading"
                  @selection-change="selsChange" style="width: 100%;">
            <el-table-column type="selection" width="55">
            </el-table-column>
            <el-table-column type="index" width="60">
            </el-table-column>
            <el-table-column prop="name" label="数据字典明细" width="160" sortable>
            </el-table-column>
            <el-table-column prop="type.name" label="数据字典类型" width="250" sortable>
            </el-table-column>
            <el-table-column label="操作">
                <template scope="scope">
                    <el-button size="small" @click="handleEdit(scope.$index, scope.row)">编辑</el-button>
                    <el-button type="danger" size="small" @click="handleDel(scope.$index, scope.row)">删除</el-button>
                </template>
            </el-table-column>
        </el-table>

        <!--工具条-->
        <el-col :span="24" class="toolbar">
            <el-button type="danger" :disabled="this.sels.length===0">批量删除</el-button>
            <el-pagination layout="prev, pager, next"
                           @current-change="handleCurrentChange"
                           :page-size="pageSize"
                           :current-page="currentPage"
                           :total="total"
                           style="float:right;">
            </el-pagination>
        </el-col>


        <!--编辑界面
          systemdictionarydetailFormVisible 模态框是否显示
          systemdictionarydetailFormRules  校验规则
          ref="systemdictionarydetailForm"  给当前表单取别名  ——> refs
        -->
        <el-dialog title="新增或编辑" :visible.sync="systemdictionarydetailFormVisible" :close-on-click-modal="false">
            <el-form :model="systemdictionarydetailForm" label-width="80px"
                     :rules="systemdictionarydetailFormRules"
                     ref="systemdictionarydetailForm">
                <el-form-item label="数据字典明细" prop="name">
                    <el-input v-model="systemdictionarydetailForm.name" auto-complete="off"></el-input>
                </el-form-item>
                <el-form-item label="数据字典类型">
                    <el-select v-model="systemdictionarydetailForm.type" value-key="id">
                        <el-option
                                v-for="item in Systemdictionarytypes"
                                :key="item.id"
                                :label="item.name"
                                :value="item">
                            <span style="float: left">{{ item.name }}</span>
                        </el-option>
                    </el-select>
                </el-form-item>
            </el-form>
            <div slot="footer" class="dialog-footer">
                <el-button @click.native="systemdictionarydetailFormVisible = false">取消</el-button>
                <el-button type="primary" @click.native="systemdictionarydetailSubmit"
                           :loading="systemdictionarydetailLoading">提交
                </el-button>
            </div>
        </el-dialog>
    </section>
</template>

<script>
    export default {
        data() {
            return {
                filters: {
                    keyword: ''
                },
                systemdictionarydetails: [],
                total: 0,
                currentPage: 1,
                pageSize: 6,
                listLoading: false,
                sels: [],//列表选中行

                systemdictionarydetailFormVisible: false,//编辑界面是否显示
                systemdictionarydetailLoading: false,//提交按钮忙等
                systemdictionarydetailFormRules: {//表单校验规则
                    name: [
                        {required: true, message: '请输入名称', trigger: 'blur'}
                    ],

                },
                //编辑界面数据
                systemdictionarydetailForm: {//表单模型数据
                    name: '',
                    type: null
                },
                Systemdictionarytypes: []
            }
        },
        methods: {
            search() {//只要是高级查询按钮一点击
                this.currentPage = 1;//定位到第一页
                this.getSystemdictionarydetails();
            },
            handleCurrentChange(val) {
                this.currentPage = val;
                this.getSystemdictionarydetails();
            },
            //获取部门列表
            getSystemdictionarydetails() {
                let para = {
                    currentPage: this.currentPage,
                    pageSize: this.pageSize,
                    keyword: this.filters.keyword
                };
                this.listLoading = true;//开启忙等框
                this.$http.post("/sysdetail", para)
                    .then(result => {
                        result = result.data;
                        this.systemdictionarydetails = result.rows;
                        this.total = result.total;
                        this.listLoading = false;

                    })
                    .catch(result => {
                        alert("系统错误！");
                    })
            },
            //删除
            handleDel(index, row) {
                this.$confirm('确认删除该记录吗?', '温馨提示', {
                    type: 'warning'
                }).then(() => {
                    this.listLoading = true;//开启忙等框
                    this.$http.delete("/sysdetail/" + row.id)
                        .then(result => {
                            this.listLoading = false;//关闭忙等框
                            //???  该完成什么事情
                            result = result.data;//AjaxResult {success:true,message:"操作成功"}
                            //AjaxResult {success:false,message:"操作失败"}
                            if (result.success) {
                                this.$message({
                                    message: result.message,
                                    type: 'success'
                                });
                                this.currentPage = 1;//定位到第一页
                                this.getSystemdictionarydetails();
                            } else {
                                this.$message({
                                    message: result.message,
                                    type: 'error'
                                });
                            }
                        })
                        .catch(result => {
                            console.log(result);
                            alert("系统错误！");
                        })
                }).catch(() => {

                });
            },
            //显示编辑界面
            handleEdit(index, row) {

                this.systemdictionarydetailFormVisible = true;//显示模态框

                this.systemdictionarydetailForm = Object.assign({}, row);//拷贝对象（回显）

                /*重置表单校验*/
                this.$refs['systemdictionarydetailForm'].resetFields();
            },
            //显示新增界面
            handleAdd() {
                this.systemdictionarydetailFormVisible = true;//显示模态框
                this.systemdictionarydetailForm = {//清空历史数据，不要ID
                    name: '',
                    type: null
                };
                /*重置表单校验*/
                this.$refs['systemdictionarydetailForm'].resetFields();
            },
            //编辑提交按钮
            systemdictionarydetailSubmit() {
                /*拿到当前vue里面的所有ref，从里面找到 systemdictionarydetailForm*/
                this.$refs.systemdictionarydetailForm.validate((valid) => {
                    if (valid) {
                        this.$confirm('确认提交吗？', '温馨提示', {})
                            .then(() => {
                                this.systemdictionarydetailLoading = true;
                                //NProgress.start();
                                //准备表单数据提交给后台
                                let para = Object.assign({}, this.systemdictionarydetailForm);
                                this.$http.put("/sysdetail", para)
                                    .then(result => {
                                        //关闭忙等框
                                        this.systemdictionarydetailLoading = false;
                                        result = result.data;
                                        if (result.success) {
                                            this.$message({
                                                message: '提交成功',
                                                type: 'success'
                                            });
                                            /*重置表单校验*/
                                            this.$refs['systemdictionarydetailForm'].resetFields();
                                            this.systemdictionarydetailFormVisible = false;
                                            /*刷新页面*/
                                            this.currentPage = 1;//定位到第一页
                                            this.getSystemdictionarydetails();
                                        } else {
                                            this.$message({
                                                message: result.message,
                                                type: 'error'
                                            });
                                            this.systemdictionarydetailFormVisible = false;
                                        }
                                    });
                            }).catch(result => {
                            console.log(result);
                            alert("系统错误！");
                            this.systemdictionarydetailFormVisible = false;
                        });
                    }
                });
            },
            selsChange: function (sels) {
                /*现目前已经选中的所有行的数据*/
                this.sels = sels;
            },
            getSystemdictionarytypes() {
                this.$http.get("/systype/list", {})
                    .then((result) => {
                        result = result.data;
                        this.Systemdictionarytypes = result
                    })
                    .catch((result) => {
                        console.log(result);
                        alert("系统错误！");
                    })
            }
        },
        mounted() {
            /**
             * 页面加载完成触发
             */
            this.getSystemdictionarydetails();
            /**
             * 页面加载获取Systemdictionarytypes
             */
            this.getSystemdictionarytypes();
        }
    }

</script>

<style scoped>

</style>